<input type="hidden" id="manage_group_status" value="<?php echo $this->session->flashdata('manage_group_status'); ?>" />
<script type="text/javascript">
  $(document).ready(function() {
    var manage_group_status = $.trim($('#manage_group_status').val());
    if (manage_group_status == 1) {
      toastr.success('Group added successfully !');
      return false;
    } else if (manage_group_status == 2) {
      toastr.error('Error occured. Try again !');
      return false;
    } else if (manage_group_status == 3) {
      toastr.success('Group deleted successfully !');
      return false;
    } else if (manage_group_status == 4) {
      toastr.success('Group updated successfully !');
      return false;
    }
  });
</script>

<div class="page" style="min-height: 500px;">
  <div class="page-container">
    <div class="container">
      <div class="row">
        <div class="span3">
          <h4 class="header">Manage Groups</h4>
          <div class="sidebar">
            <ul class="col-nav span3">
              <li><a style="background-color: #EEE;" href="<?php echo base_url('groups_manage/group/'); ?>"><i class="pull-right icon-th-large"></i>Manage Groups</a></li>
              <li><a href="<?php echo base_url('groups_manage/group_member/'); ?>"><i class="pull-right icon-user"></i>Manage Group Members</a></li>
              <li><a href="<?php echo base_url('groups_manage/group_event/'); ?>"><i class="pull-right icon-th-list"></i>Manage Group Events</a></li>
              <li><a href="<?php echo base_url('groups_manage/group_event_result/'); ?>"><i class="pull-right icon-signal"></i>Manage Group Event Results</a></li>
              <li><a href="<?php echo base_url('groups_manage/api_apps/'); ?>"><i class="pull-right icon-fire"></i>API Apps</a></li>
            </ul>
          </div>
        </div>
        <div class="span9">
          <div class="btn-group pull-right">
            <a href="#add_group" data-toggle="modal" class="btn">Add Group</a>
            <a title="<?php echo 'Can accessed by : @' . @implode(', @', $this->comitte_members); ?>" href="<?php echo base_url('groups_manage'); ?>" class="btn">Manage groups</a>
          </div>
          <h4 class="header">Groups [<?php echo NOW_YEAR; ?>]</h4>
          <?php if (!empty($groups)) { ?>
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th width="5%">#</th>
                  <th width="50%">Group name</th>
                  <th width="5%">Year</th>
                  <th width="5%">Score</th>
                  <th width="10%">Capitan</th>
                  <th width="10%">Vice Capitan</th>
                  <th width="10%">Comitte Member</th>
                  <th width="5%">Action</th>
                </tr>
              </thead>
              <tbody>
                <?php $count = 1; ?>
                <?php foreach ($groups as $group) { ?>
                  <tr>
                    <td><?php echo $count; ?></td>
                    <td><?php echo $group['group_name']; ?></td>
                    <td><?php echo $group['group_year']; ?></td>
                    <td><?php echo $group['group_score']; ?></td>
                    <td>
                      <?php $user_detatils = get_user_details_by_id($group['group_c_user_id']); ?>
                      <?php if (!empty($user_detatils)) { ?>
                        <?php echo '@' . $user_detatils['user_name']; ?>
                      <?php } else { ?>
                        --
                      <?php } ?>
                    </td>
                    <td>
                      <?php $user_detatils = get_user_details_by_id($group['group_vc_user_id']); ?>
                      <?php if (!empty($user_detatils)) { ?>
                        <?php echo '@' . $user_detatils['user_name']; ?>
                      <?php } else { ?>
                        --
                      <?php } ?>
                    </td>
                    <td>
                      <?php $user_detatils = get_user_details_by_id($group['group_cm_user_id']); ?>
                      <?php if (!empty($user_detatils)) { ?>
                        <?php echo '@' . $user_detatils['user_name']; ?>
                      <?php } else { ?>
                        --
                      <?php } ?>
                    </td>
                    <td>
                      <a href="#update_group_<?php echo $group['group_id']; ?>" data-toggle="modal" title="Update group" style="text-decoration: none; color: skyblue; cursor: pointer;"><i class="icon-large icon-pencil"></i></a>
                      &nbsp;|&nbsp;
                      <a title="Delete group" style="text-decoration: none; color: tomato; cursor: pointer;" onclick="return confirm_action('Delete group ?');" href="<?php echo site_url('groups_manage/group_delete/' . $group['group_id']); ?>"><i class="icon-large icon-trash"></i></a>
                    </td>
                  </tr>
                  <?php $count++; ?>
                <?php } ?>
              </tbody>
            </table>
          <?php } else { ?>
            <div class="alert alert-danger">No groups !</div>
          <?php } ?>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="add_group" class="modal hide fade">
  <div class="modal-header">
    <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
    <h3>Add group [<?php echo NOW_YEAR; ?>]</h3>
  </div>
  <form class="form-horizontal" method="post" action="<?php echo base_url('groups_manage/group'); ?>">
    <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
    <div class="modal-body">
      <div class="control-group">
        <label class="control-label">Group Name</label>
        <div class="controls">
          <input name="add_group_name" type="text" placeholder="Group name (250)" required="required" maxlength="250"/>
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <button data-dismiss="modal" class="btn">Close</button>
      <button type="submit" class="btn btn-primary">Save</button>
    </div>
  </form>
</div>

<?php if (!empty($groups)) { ?>
  <?php foreach ($groups as $group) { ?>
    <div id="update_group_<?php echo $group['group_id']; ?>" class="modal hide fade">
      <div class="modal-header">
        <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
        <h3>Update group</h3>
      </div>
      <form class="form-horizontal" method="post" action="<?php echo base_url('groups_manage/group_update/' . $group['group_id']); ?>">
        <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
        <div class="modal-body">
          <div class="control-group">
            <label class="control-label">Group Name</label>
            <div class="controls">
              <input name="update_group_name" type="text" value="<?php echo $group['group_name']; ?>" placeholder="Group name (250)" required="required" maxlength="250"/>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button data-dismiss="modal" class="btn">Close</button>
          <button type="submit" class="btn btn-primary">Save</button>
        </div>
      </form>
    </div>
  <?php } ?>
<?php } ?>